1 =============================================================================
2 Windows APPLICATION: VBExtractAudioFile Overview
3 =============================================================================
5 /////////////////////////////////////////////////////////////////////////////
8 The sample demonstrates how to extract and convert audio file formats, which
9 include wav, mp3 and mp4 files.
11 The sample is used to extract music file formats. We usually play music with
12 Windows Media Player or other music-playing software. If we find our favorite
13 music clip, we can use the function of this sample to extract it and convert
14 it into another file format. All technology mentioned above is based on
15 Expression Encoder SDK 4.0. When you install Expression Encoder 4.0, you can
16 use Visual Studio 2010 to add a reference to it. In this way, you don't have
17 to install the SDK installation kits individually.
19 The sample uses Expression Encoder SDK 4 to output *.mp4 or *.wma file. The
20 .mp3 audio format is currently not supported as output format in Expression
24 /////////////////////////////////////////////////////////////////////////////
27 1. Please install a free version, called "Microsoft Expression Encoder 4" at
29 http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=75402be0-c603-4998-a79c-becdd197aa79
31 The free version is a feature-filled VC-1 encoding application that supports
34 - High performance multi-core encoding
35 - Crop/scale/de-interlace operation
39 - Up to 10 minutes of screen capture
41 - Silverlight templates
42 - Multi-channel audio import and export
43 - Rich metadata support
44 - Presets and custom plug-ins as well as
45 - Full access to the .NET SDK for all above features
46 - VC-1 Smooth Streaming (new for v4)
48 The Professional version of Microsoft Expression Encoder 4 adds:
49 - H.264 encoding (both MP4 and Smooth Streaming)
50 - Live Smooth Streaming encoding
52 - Native support of MP4/H.264, TS, M2TS, AVCHD, MPEG-2, ISM, ISMV, AAC
54 - Unlimited screen capture durations
56 So if you like these features provided by this software application, please
57 purchase the professional version to support more advanced features.
60 /////////////////////////////////////////////////////////////////////////////
63 Step1. Install Microsoft Expression Encoder 4.
65 Step2. Build and run the sample project in Visual Studio 2010.
67 Step3. Prepare a .wma or .mp4 or .mp3 audio file. Click the button which is
68 written as "Choose Audio File..." and select the audio file.
70 Step4. Click the play button and drag the block on the progress bar to
71 seek the position that you want to start extracting. Then, you click
72 the "Set Start Point" button.
73 Do the same operation to set the end of extract file. It's important
74 that you don't set the extract end point before the start point.
76 Step5. Select the audio output file format between WMA and MP4. Set the output
79 Step6. Click the "Extract" button to extract the audio file. If it succeeds,
80 you will be informed the output file path. You can play the output
81 audio file to check if the audio clip is correct.
84 /////////////////////////////////////////////////////////////////////////////
87 1. The sample uses the Windows Media Player control to play the source audio
88 file. The following MSDN article introduces how to embed the Windows
89 Media Player ActiveX control in a Windows Form.
90 http://msdn.microsoft.com/en-us/library/dd562851.aspx
92 We play a specified audio file in the Windows Media Player control by
93 setting its URL property and calling AxWindowsMediaPlayer.Ctlcontrols.play().
95 Me.player.URL = openAudioFileDialog.FileName
96 Me.player.Ctlcontrols.play()
98 When the form is being closed, we call the AxWindowsMediaPlayer.close()
99 method to release Windows Media Player resources.
101 Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
102 ' Releases Windows Media Player resources.
106 In order to get the current playing position in the media item, and save
107 the start and end points, we use the
108 AxWindowsMediaPlayer.Ctlcontrols.currentPosition property. It contains the
109 current position in the media item in seconds from the beginning:
111 If btnSetBeginEndPoints.Tag.Equals("SetStartPoint") Then
112 ' Save the startpoint.
113 ' player.Ctlcontrols.currentPosition contains the current
114 ' position in the media item in seconds from the beginning.
115 Me.tbStartpoint.Text = (player.Ctlcontrols.currentPosition * 1000).ToString("0")
116 Me.tbEndpoint.Text = ""
118 Me.btnSetBeginEndPoints.Text = "Set End Point"
119 Me.btnSetBeginEndPoints.Tag = "SetEndPoint"
120 ElseIf btnSetBeginEndPoints.Tag.Equals("SetEndPoint") Then
121 ' Check if the startpoint is in front of the endpoint.
122 Dim endPoint As Integer = CInt(Fix(player.Ctlcontrols.currentPosition * 1000))
123 If endPoint <= Integer.Parse(Me.tbStartpoint.Text) Then
124 MessageBox.Show("Audio endpoint is overlapped. Please reset the endpoint.")
127 Me.tbEndpoint.Text = endPoint.ToString()
129 Me.btnSetBeginEndPoints.Text = "Set Start Point"
130 Me.btnSetBeginEndPoints.Tag = "SetStartPoint"
134 2. The sample uses following helper function to cut the audio file from
135 startpoint to endpoint, and output the clip as the selected audio format.
137 Public Shared Function ExtractAudio(ByVal sourceAudioFile As String, ByVal outputDirectory As String, ByVal outputType As OutputAudioType, ByVal startpoint As Double, ByVal endpoint As Double) As String
138 Using [job] As New Job()
139 Dim src As New MediaItem(sourceAudioFile)
140 Select Case outputType
141 Case OutputAudioType.MP4
142 src.OutputFormat = New MP4OutputFormat()
143 src.OutputFormat.AudioProfile = New AacAudioProfile()
144 src.OutputFormat.AudioProfile.Codec = AudioCodec.AAC
145 src.OutputFormat.AudioProfile.BitsPerSample = 24
146 Case OutputAudioType.WMA
147 src.OutputFormat = New WindowsMediaOutputFormat()
148 src.OutputFormat.AudioProfile = New WmaAudioProfile()
149 src.OutputFormat.AudioProfile.Bitrate = New VariableConstrainedBitrate(128, 192)
150 src.OutputFormat.AudioProfile.Codec = AudioCodec.WmaProfessional
151 src.OutputFormat.AudioProfile.BitsPerSample = 24
154 Dim spanStart As TimeSpan = TimeSpan.FromMilliseconds(startpoint)
155 src.Sources(0).Clips(0).StartTime = spanStart
156 Dim spanEnd As TimeSpan = TimeSpan.FromMilliseconds(endpoint)
157 src.Sources(0).Clips(0).EndTime = spanEnd
159 [job].MediaItems.Add(src)
160 [job].OutputDirectory = outputDirectory
163 Return [job].MediaItems(0).ActualOutputFileFullPath
168 btnExtract_Click in MainForm.cs calls the helper method to extract the audio file.
170 Dim outputType As OutputAudioType = CType(Me.cmbOutputAudioType.SelectedValue, OutputAudioType)
171 Dim outputFileName As String = ExpressionEncoderWrapper.ExtractAudio(sourceAudioFile, outputDirectory, outputType, Double.Parse(startpoint), Double.Parse(endpoint))
175 //////////////////////////////////////////////////////////////////////////////////////
178 Expression Encoder SDK Programming Reference
179 http://msdn.microsoft.com/en-us/library/ff396833(v=Expression.40).aspx
181 Microsoft Expression Encoder 4 FAQ
182 http://social.expression.microsoft.com/Forums/en/encoder/thread/3eabf903-b49f-4f92-b508-f28a795d6c90
185 //////////////////////////////////////////////////////////////////////////////////////